<?php
// +----------------------------------------------------------------------
// | SMEOA [ 专注小微企业信息化 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.smeoa.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: smeoa <smeoa@qq.com>
// +----------------------------------------------------------------------

class CloginAction extends Action {
	// 检查用户是否登录

	public function index() {
		//如果通过认证跳转到首页
		//redirect(__APP__);
		if (!$this -> _session(C('USER_AUTH_KEY'))) {
			$this -> display();
		} else {
			//echo __APP__;
			redirect(U("/main/index"));
		}
	}

	// 用户登出
	public function logout() {
		if ($this -> _session(C('USER_AUTH_KEY'))){
			session(C('USER_AUTH_KEY'), null);
			//die();
                        unset($_SESSION);
			$this -> assign("jumpUrl", __URL__ . '/');
			$this -> success('登出成功！');
		} else {
                        unset($_SESSION);
			$this -> assign("jumpUrl", __URL__ . '/');
			$this -> error('已经登出！');
		}
	}

	// 登录检测
	public function check_login() {
		if (empty($_POST['name'])) {
			$this -> error('帐号必须！');
		} elseif (empty($_POST['password'])) {
			$this -> error('密码必须！');
		}
		//生成认证条件
		$map = array();
		// 支持使用绑定帐号登录
		$map['name'] = $_POST['name'];
		$map["status"] = array('gt', 0);
		import('@.ORG.Util.RBAC');
		$customerInfo = RBAC::authenticate($map,'customer');
		//使用用户名、密码和状态的方式进行认证
		if (false === $customerInfo) {
			$this -> error('帐号或密码错误！');
		} else {
			if ($customerInfo['password'] != md5($_POST['password'])) {
				$this -> error('帐号或密码错误！');
			}
			session(C('USER_AUTH_KEY'), $customerInfo['id']);
			session('name', $customerInfo['name']);
			session('email', $customerInfo['email']);
                        session('role_ids', $customerInfo['role_ids']);
                        session('login_customer', '1');
                        $model_node = D('Node');
                        $check_nodes = $model_node->get_checked_nodes();
                        session('check_nodes',$check_nodes);
 
                        //var_dump($customerInfo);
			//读取数据库模块列表生成菜单项
			$menu = D("Node") -> access_customer_list($customerInfo['id'],$customerInfo['role_ids']);

			//缓存菜单访问
			session('menu' . $customerInfo['id'], $menu);
			//保存登录信息
			$User = M('customer');
			$ip = get_client_ip();
			$time = time();
			$data = array();
			$data['id'] = $customerInfo['id'];
			$data['last_login_time'] = $time;
			$data['login_count'] = array('exp', 'login_count+1');
			$data['last_login_ip'] = $ip;
			$User -> save($data);
			$this -> assign('jumpUrl',U("/camera/index"));
			$this -> success('登录成功！');
		}
	}

	// 更换密码
	public function change_pwd() {
		$this -> checkUser();
		//对表单提交处理进行处理或者增加非表单数据
		//if(md5($_POST['verify'])	!= $_session['verify']) {
		//	$this->error('验证码错误！');
		//}
		$map = array();
		$map['password'] = pwdHash($_POST['oldpassword']);
		if (isset($_POST['account'])) {
			$map['account'] = $_POST['account'];
		} elseif (($this ->get_user_id())) {
			$map['id'] = $this ->get_user_id();
		}
		//检查用户
		$User = M("User");
		if (!$User -> where($map) -> field('id') -> find()) {
			$this -> error('旧密码不符或者用户名错误！');
		} else {
			$User -> password = pwdHash($_POST['password']);
			$User -> save();
			$this -> assign('jumpUrl',$this->get_return_url());	
			$this -> success('密码修改成功！');
		}
	}

	public function profile() {
		$this -> checkUser();
		$User = M("User");
		$vo = $User -> getById($this ->get_user_id());
		$this -> assign('vo', $vo);
		$this -> display();
	}

	public function verify() {
		$type = isset($_GET['type']) ? $_GET['type'] : 'gif';
		import("@.ORG.Util.Image");
		Image::buildImageVerify(4, 1, $type);
	}
        
        public  function register(){
            $this->display();
        }
        
        public  function register_done(){
            
                $ajax = $_POST['ajax'];
		$model = D('Clogin');
		if (false === $model -> create()) {
			$this -> error($model -> getError());
		}
                $password = Rv('post','password');
                $repassword = Rv('post','repassword');
                $name = Rv('post','name');
                $customer_type = Rv('post','customer_type');
                $biz_type = Rv('post','biz_type');
                $biz_license = Rv('post','biz_license');
                $email = Rv('post','email');
                $mobile_tel = Rv('post','mobile_tel');
                $im = Rv('post','im');
                
                
                if( $password!= $repassword){
                    $data['info']= '密码不一致';
                    $data['state'] = 0;
                    $this -> ajaxReturn($data,'JSON');
                    exit;
                }
                $model -> __set('name', $name);
                $model -> __set('customer_type', $customer_type);
                $model -> __set('biz_type', $biz_type);
                $model -> __set('biz_license', $biz_license);
                $model -> __set('email', $email);
                $model -> __set('mobile_tel', $mobile_tel);
                $model -> __set('im', $im);
		$model -> __set('letter', get_letter($name));
                $model -> __set('password', pwdHash($password));
                $model -> __set('addtime', time());
                $model -> __set('user_id', 1);
                $model -> __set('dept_id', 1);
		//保存当前数据对象
		$list = $model->table('think_customer')-> add();
		if ($list !== false) {//保存成功
                    $customerInfo = RBAC::authenticate($map,'customer');
                        $customerInfo['id'] =  $model->getLastInsID();
                        $customerInfo['role_ids'] = 10;
                    	session(C('USER_AUTH_KEY'),$customerInfo['id']);
			session('name', $name);
			session('email', $email);
                        session('role_ids', $customerInfo['role_ids']);
                        session('login_customer', '1');
                        $model_node = D('Node');
                        $check_nodes = $model_node->get_checked_nodes();
                        session('check_nodes',$check_nodes);
 
                        //var_dump($customerInfo);
			//读取数据库模块列表生成菜单项
			$menu = D("Node") -> access_customer_list($customerInfo['id'],$customerInfo['role_ids']);

			//缓存菜单访问
			session('menu' . $customerInfo['id'], $menu);
                    
                    
		   $data['info']= '注册成功';
                   $data['state'] = '1';
                   $data['user_info'] = $customerInfo;
		} else {
			//失败提示
                    $data['info']= '注册失败';
                    $data['state'] = '0';
		}
                 $this -> ajaxReturn($data, "json");
            
        }
}
?>